import paramiko
import time
import socket

devicelist = \
    [{'name':'SW1','ip':'203.0.113.1','username':'qytang','password':'qytang','platform':'IOS'},\
     {'name':'SW2','ip':'203.0.113.2','username':'qytang','password':'qytang','platform':'IOS'},\
     {'name':'SW3','ip':'203.0.113.3','username':'qytang','password':'qytang','platform':'VRP'},\
     {'name':'SW4','ip':'203.0.113.4','username':'qytang','password':'qytang','platform':'VRP'}]

unreachable_list=[]
auth_fail_list=[]

def cisco(ipt,usernamet,passwordt):
    ssh1=paramiko.SSHClient()
    ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh1.connect(hostname=ipt,port=22,username=usernamet,password=passwordt,timeout=5,\
                 allow_agent=False,look_for_keys=False)
    cli_connect=ssh1.invoke_shell()
    cli_connect.send('enable\n')
    time.sleep(1)
    cli_connect.send('qytang\n')
    time.sleep(1)
    cli_connect.send('terminal length 0\n')
    time.sleep(1)
    cli_connect.send('show run\n')
    time.sleep(1)
    run_config=cli_connect.recv(99999).decode()
    time.sleep(1)
    ssh1.close()
    return run_config

def huawei(ipt,usernamet,passwordt):
    ssh1=paramiko.SSHClient()
    ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh1.connect(hostname=ipt,port=22,username=usernamet,password=passwordt,timeout=5,\
                 allow_agent=False,look_for_keys=False)
    cli_connect=ssh1.invoke_shell()
    cli_connect.send('screen-length 0 temporary\n')
    time.sleep(1)
    cli_connect.send('dis cur\n')
    time.sleep(1)
    run_config=cli_connect.recv(99999).decode()
    time.sleep(1)
    ssh1.close()
    return run_config

def config_bkp(namet,configt):
    config = open('d:/michael/Videos/' + namet +'_'+ '.txt', mode='w+')
    config.write(configt)
    config.close()
    print(namet + ' config backup finished')

for device in range(len(devicelist)):
    plat = devicelist[device]['platform']
    name1 = devicelist[device]['name']
    ip1 = devicelist[device]['ip']
    username1 = devicelist[device]['username']
    password1 = devicelist[device]['password']
    try:
        if plat == 'IOS':
            running = cisco(ip1,username1,password1)
        elif plat == 'VRP':
            running = huawei(ip1,username1,password1)
    except socket.error:
        unreachable_list.append(ip1)
    except paramiko.ssh_exception.AuthenticationException:
        auth_fail_list.append(name1)
    else:
        config_bkp(name1, running)
for i in unreachable_list:
    result = open('d:/michael/Videos/unreachable_list.txt', mode='w+')
    result.write(i + ' - is unreachable')
    result.close()
    print(i + ' - is unreachable')
for i in auth_fail_list:
    result = open('d:/michael/Videos/auth_fail_list.txt', mode='a')
    result.write(i + ' - 由于某种原因身份验证失败引发异常')
    result.close()
    print(i + ' - 由于某种原因身份验证失败引发异常')
else:
    print('='*80)
    print('Mission accomplished.')
    print('='*80)

# paramiko常见异常参考
#http://docs.paramiko.org/en/stable/api/ssh_exception.html
